package com.kgc.springboot.hangduo.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.pojo.Catalog;
import com.kgc.springboot.hangduo.pojo.Laws;
import com.kgc.springboot.hangduo.service.CatalogService;
import com.kgc.springboot.hangduo.service.LawsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ASUS
 * @date: 2020/12/27 14:36
 * @description:
 */
@Controller
public class CatalogController {
    @Autowired
    private CatalogService catalogService;
    @Autowired
    LawsService lawsService;

    @RequestMapping("/mulu-list.html")
    public String selectLevel(@RequestParam(value = "lid",required = false,defaultValue = "0")Integer lid, Model model){

        List<Laws> laws = lawsService.selectAll();
        model.addAttribute("lawsList",laws);
        PageInfo<Catalog> pageInfo = catalogService.selectAll(lid);
//        for (Catalog catalog : pageInfo.getList()) {
//            System.out.println(catalog);
//        }
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("lid",lid);

        return "mulu-list";
    }

    @RequestMapping("/addTopmulu")
    public String addTopmulu(@RequestParam("lawid")Integer lawid,Model model){
        model.addAttribute("lawid",lawid);
        PageInfo<Catalog> pageInfo = catalogService.selectAll(lawid);
        int size = pageInfo.getList().size();
        model.addAttribute("size",size);
        System.out.println(lawid);
        System.out.println(size);
        return "mulu-add";
    }
    @ResponseBody
    @RequestMapping("/addCatalog")
    public String addCatalog(Catalog catalog,Model model){
        System.out.println(catalog);
        int i = catalogService.addCatalog(catalog);

        if (i>0){
            return "<script language='javascript'>" +
                    "var index = parent.layer.getFrameIndex(window.name);\n" +
                    "parent.layer.close(index);" +
                    "window.location.href='/mulu-list.html';" +
                    "alert('添加成功');" +
                    "</script>";
        }else{
            return "<script language='javascript'>" +
                    "var index = parent.layer.getFrameIndex(window.name);\n" +
                    "parent.layer.close(index);" +
                    "window.location='/mulu-list.html';" +
                    "alert('添加失败');" +
                    "</script>";
        }

    }

    @RequestMapping("/delCatalog")
    public String delCatalog(@RequestParam("id")Integer id,
                             @RequestParam(value = "lid",required = false)Integer lid,Model model){
        int i = catalogService.delCatalog(id);
        return "redirect:/mulu-list.html?lid="+lid;
    }

    @RequestMapping("/updateCatalog")
    public String updateCatalog(@RequestParam("id")Integer id,
                             @RequestParam(value = "lid",required = false)Integer lid,Model model){
        Catalog catalog = catalogService.selectById(id);
        Laws laws = lawsService.selectLawsById(lid);
        model.addAttribute("catalog",catalog);
        model.addAttribute("laws",laws);
        return "mulu-update";
    }
    @RequestMapping("/updateCatalogById")
    public String updateCatalogById(Catalog catalog,Model model){
        System.out.println(catalog);
        catalogService.updateCatalogById(catalog);
        return "redirect:/mulu-list.html?lid="+catalog.getLawsId();
    }
    @RequestMapping("/addCatalogById")
    public String addCatalogById(@RequestParam("id")Integer id,
                                 @RequestParam(value = "lid",required = false)Integer lid,Model model){
        Catalog catalog = catalogService.selectById(id);
        System.out.println("子级目录："+catalog);
        if(catalog.getCatalogList()!=null&&catalog.getCatalogList().size()>0){
            int size = catalog.getCatalogList().size();
            model.addAttribute("sequence",size+1);
        }else {
            model.addAttribute("sequence",1);
        }
        model.addAttribute("catalog",catalog);

        return "zimulu-add";
    }


    @RequestMapping("/addZiJiCatalog")
    public String addZiJiCatalog(Catalog catalog,Model model){
        System.out.println(catalog);
        int i = catalogService.addCatalog(catalog);
        return "redirect:/mulu-list.html?lid="+catalog.getLawsId();
    }

    //********************************************前端功能**********************************************

    @RequestMapping("/selectMuLuByLawId")
    public String  selectMuLuByLawId(@RequestParam("lid")Integer lid, Model model){

        PageInfo<Catalog> pageInfo = catalogService.selectAll(lid);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("lid",lid);
        return "user-mulu";
    }

    @ResponseBody
    @RequestMapping("/selectByLawId")
    public Map<String,Object> selectByLawId(@RequestParam("lid")Integer lid){
        Map<String,Object> map = new HashMap<>();
        List<Catalog> catalogs = catalogService.selectByLawId(lid);
        if (catalogs !=null && catalogs.size()>0){
            map.put("info",catalogs);
            map.put("status",1);
        }else{
            map.put("info","暂无目录");
            map.put("status",0);
        }
        return map;
    }
}
